//
//  LLActionSnapshotView.swift
//  liliiosPorject
//
//  Created by liuzhu on 2018/12/26.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class LLActionSnapshotView: UIView {

    var backDay: ((LLDay) -> Void)?
    public var day:LLDay?{
        didSet{
            
            let currentDateStr = LLDateUtil.shareLLDateUtil().dateConverString(date: LLDateUtil.shareLLDateUtil().getCurrentDate(), dateFormat: "yyyy-MM-dd")
            if currentDateStr == day?.dateStr{
                DispatchQueue.main.async {
                    self.dateLabe.text = "今日"
                }
                
            }else{
                dateLabe.text = day?.dateStr
            }
            let count = day?.actions.count
            switch count {
            case 0:
                self.activeView1.content = ""
                self.activeView2.content = "没有日程安排"
                self.activeView3.content = ""
                break
            case 1:
                let action = self.day?.actions.first
                self.activeView1.content = ""
                self.activeView2.content = action?.titleName
                self.activeView3.content = ""
                break
            case 2:
                let action1 = self.day?.actions.first
                let action2 = self.day?.actions.last
                self.activeView1.content = action1?.titleName
                self.activeView2.content = action2?.titleName
                self.activeView3.content = ""
                break
            default:
                let action1 = self.day?.actions.first
                let action2 = self.day?.actions[1]
                let action3 = self.day?.actions[2]
                self.activeView1.content = action1?.titleName
                self.activeView2.content = action2?.titleName
                self.activeView3.content = action3?.titleName
                break
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = LLConst.backgroundGray()
        self.addSubview(titleView)
        self.addSubview(activeView1)
        self.addSubview(activeView2)
        self.addSubview(activeView3)
        self.addSubview(moreView)
        self.addSubview(moreBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var dateLabe:UILabel = {
        let la = UILabel(frame:CGRect(x: 0, y: 0, width: self.bounds.size.height - 40, height: self.bounds.size.height - 40))
        la.textAlignment = NSTextAlignment.center
        la.font = UIFont.systemFont(ofSize: 18)
        la.textColor = UIColor.darkGray
        la.text = "今日"
        la.numberOfLines = 0
        return la
    }()
    
    private lazy var titleView:UIView = {
        let ve = UIView(frame: CGRect(x: 20, y: 20, width: self.bounds.size.height - 40, height: self.bounds.size.height - 40))
        ve.backgroundColor = LLConst.lightGray()
        ve.addSubview(dateLabe)
        return ve
    }()
    
    
    
    public lazy var activeView1:ActiveView = {
       let ve = ActiveView(frame: CGRect(x: titleView.bounds.size.width+30, y: 20, width: self.bounds.size.width - (50+titleView.bounds.size.width), height: titleView.bounds.size.height/3))
        ve.content = ""
       return ve
    }()
    
    public lazy var activeView2:ActiveView = {
        let ve = ActiveView(frame: CGRect(x: titleView.bounds.size.width+30, y: activeView1.frame.origin.y + (titleView.bounds.size.height/3) , width: self.bounds.size.width - (50+titleView.bounds.size.width), height: titleView.bounds.size.height/3))
        ve.content = "没有日程安排"
        return ve
    }()
    
    public lazy var activeView3:ActiveView = {
        let ve = ActiveView(frame: CGRect(x: titleView.bounds.size.width+30, y: activeView2.frame.origin.y + (titleView.bounds.size.height/3) , width: self.bounds.size.width - (50+titleView.bounds.size.width), height: titleView.bounds.size.height/3))
        ve.content = ""
        return ve
    }()
    
    private lazy var moreView:UIImageView = {
        let iv = UIImageView.init(frame: CGRect(x: self.bounds.size.width-20, y: (self.bounds.size.height - 40)/2, width: 20, height: 40))
        iv.image = UIImage(named: "more")
        return iv
    }()
    
    private lazy var moreBtn:UIButton = {
        let bt = UIButton.init(frame: CGRect(x: self.bounds.size.width-40, y: 0, width: 40, height: self.bounds.size.height))
        bt.backgroundColor = UIColor.clear
        bt.addTarget(self, action: #selector(moreAction), for: UIControl.Event.touchUpInside)
        return bt
    }()
    
    @objc func moreAction(){
        let count = day?.actions.count ?? 0
        if count <= 0 {
            SVProgressHUD.showError(withStatus: "今天没有日程安排")
            return
        }
        backDay!(day!)
    }
}

class ActiveView: UIView {
    
    public var content:String?{
        didSet{
            if content != nil && content!.count != 0 {
                self.isHidden = false
                self.actionLabel.text = content
            }else{
                self.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(pointView)
        self.addSubview(actionLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public lazy var pointView:UIImageView = {
        let iv = UIImageView.init(frame: CGRect(x: 0, y:(self.bounds.size.height-10)/2, width: 10, height: 10))
        iv.image = UIImage(named: "point")
        return iv
    }()
    
    public lazy var actionLabel:UILabel = {
        let la = UILabel.init(frame: CGRect(x: pointView.frame.origin.x+20, y: 0, width: self.bounds.size.width - 20, height: self.bounds.size.height))
        return la
    }()
}
